const storageKeyPrefix = 'datatrade_'

class Storage {
  constructor (storage) {
    this._storage = storage
  }

  set (key, item) {
    this._storage.setItem(storageKeyPrefix + key, JSON.stringify(item))
  }

  get (key) {
    let val = this._storage.getItem(storageKeyPrefix + key)
    try {
      return JSON.parse(val)
    } catch (ex) {
      console.log(ex)
      return val
    }
  }

  remove (key) {
    this._storage.removeItem(storageKeyPrefix + key)
  }

  clear () {
    this._storage.clear()
  }
}

export const local = new Storage(localStorage)

export const session = new Storage(sessionStorage)

export default {local, session}
